<?php
namespace app\zhengmao\controller;

use app\zhengmao\model\BannerModel;
use cmf\controller\AdminBaseController;
use think\facade\Db;

class BannerController extends AdminBaseController
{
    public function index()
    {
        $title= input('title');
        $where = [];
        if($title){
            $where[] = ['title','like',"%$title%"];
        }
        $data = BannerModel::order('list_order','desc')
            ->where($where)
            ->paginate('10');;
        return $this->fetch('index',[
            'data' => $data
        ]);
    }
    public function add()
    {
        return $this->fetch('add');
    }
    public function addPost()
    {
        $image = $_POST['image'];
        $image = cmf_get_image_url($image);
        $check = Db::name('banner')->save([
            'image'=>$image,
            'url'=>'#'
        ]);
        if($check){
            $this->success('添加成功');
        }
    }
    public function edit()
    {
        $id=input('id');
        $data = BannerModel::find($id);
        return $this->fetch('edit',[
            'data' => $data
        ]);
    }
    public function editPost()
    {
        $data = input();
        $save = BannerModel::update($data);
        if($save){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }
    public function delete()
    {
        $id=input('id');
        $delete = BannerModel::destroy($id);
        if($delete){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    public function list_order()
    {
        $model= new BannerModel();
        parent::listOrders($model);
        $this->success('排序成功');
    }
}
